﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Bule.Malbilden.Data;
using Bule.Malbilden.Domain;

namespace Bule.Malbilden.Forms
{
    public class ListViewItemListCreator : IListViewItemListCreator
    {
        private IPersistentContext context;

        public ListViewItemListCreator(IPersistentContext context)
        {
            this.context = context;
        }

        public List<ListViewItem> CreateFoodProductList()
        {
            return CreateList<FoodProduct>();
        }

        public List<ListViewItem> CreateMealList()
        {
            return CreateList<Meal>();
        }

        public List<ListViewItem> CreateEatableList()
        {
            return CreateList<IEatable>();
        }

        private List<ListViewItem> CreateList<T>() where T : IEatable
        {
            var itemList = new List<ListViewItem>();
            ICollection<T> persisted = context.GetAll<T>();

            foreach (var eatable in persisted)
                itemList.Add(new ListViewItem(eatable.Name) { Tag = eatable });

            return itemList;
        }
    }
}
